import requests
import pprint
from pymongo import MongoClient

#将数据保存到Mongodb中
client=MongoClient()
#创建数据库
database=client['weibo--user']
#创建集合
db=database['user']
header={
'User-Agent':'Chrome/98.0.4758.102 Mobile Safari/537.36',
'content-type':'application/json; charset=utf-8'
}
#爬取作者个人微博

all_data=[]
# page=input('请输入页码:')
for page in range(1,5):
    url='https://m.weibo.cn/api/container/getIndex?jumpfrom=weibocom&type=uid&value=1768167034&containerid=1076031768167034&page={}'.format(page)
    response=requests.get(url,headers=header)
    #获取json格式数据
    data=response.json()
    cars=data['data']['cards']
    # pprint.pprint(cars)
    for i in cars:
        mlog=i.get('mblog',None)
        if mlog:
              df={}
    #         有内容才提取
              df['user_name'] = mlog.get('user', {}).get('screen_name', None)
              mid = mlog.get('mid', None)
              df['mid']=mid
              df['source'] = mlog.get('source', None)
              df['text']=mlog.get('text',None)

              # all_data.append(df)
              #评论下面的评论
              coment_url='https://m.weibo.cn/comments/hotflow?&mid={}&max_id_type=0'.format(mid)
              comment=requests.get(coment_url,headers=header).json()
              # pprint.pprint(comment)
              mql=comment.get('data',{}).get('data')
              b=[]
              for p in mql:
                  #获取数据
                  comment_text=p.get('text')
                  comment_mid=p.get('mid')
                  comment_username=p.get('user',{}).get('screen_name',None)
                  b.append([comment_username,comment_mid,comment_text])
                  df['comment']=b

              all_data.append(df)


#插入数据
db.insert_many(all_data)

#验证
# content=[x for x in db.find()]
# print(content)